<%
# Copyright (C) 2011 - present Instructure, Inc.
#
# This file is part of Canvas.
#
# Canvas is free software: you can redistribute it and/or modify it under
# the terms of the GNU Affero General Public License as published by the Free
# Software Foundation, version 3 of the License.
#
# Canvas is distributed in the hope that it will be useful, but WITHOUT ANY
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
# A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
# details.
#
# You should have received a copy of the GNU Affero General Public License along
# with this program. If not, see <http://www.gnu.org/licenses/>.
%>

<%
  currently_peer_reviewing = @assessment_request.try(:assigned?)
  js_bundle :submissions
  css_bundle :submission
  js_env({
    :RUBRIC_ASSESSMENT => {
      :assessment_user_id => @submission.user_id,
      :assessor_id => @current_user ? @current_user.id : "null",
      :assessment_type => (!currently_peer_reviewing && can_do(@submission, @current_user, :grade)) ? "grading" : "peer_review"
    },

    :SUBMISSION => {
      :user_id => @submission.user_id,
      :assignment_id => @assignment.id,
      # don't leak grades if assignment is muted
      :submission => @assignment.muted? ? {} : @submission.attributes.merge(entered_grade: @submission.entered_grade, entered_score: @submission.entered_score)
    }
  })
  content_for :page_title, join_title(@assignment.title, submission_author_name_for(@assessment_request))
  add_crumb(t('crumbs.assignments', "Assignments"), context_url(@context, :context_assignments_url))
  add_crumb(@assignment.title, context_url(@context, :context_assignment_url, @assignment.id))
  add_crumb(user_crumb_name)
  @active_tab = "assignments"
%>
<% content_for :stylesheets do %>
  <style>
    .rubric {
      max-width: 600px;
    }
  </style>
<% end %>

<% if @assignment.omit_from_final_grade %>
  <div class="alert alert-info omit-from-final-warning">
    This assignment does not count towards the final grade.
  </div>
<% end %>
  <div class="submission-details-header submission_details">
    <div class="submission-details-header__grade-summary">
      <% if @assignment.muted? %>
        <p style="width: 200px">{{#t}}Grades are unavailable because the instructor is working on them.{{/t}}</p>
      <% elsif @submission.user_can_read_grade?(@current_user, session) %>
      <table class="grade-values">
        <tbody>
          <tr>
            <th><%= I18n.t('Grade:') %></td>
            <td class="bold">
              <% if can_do(@assignment, @current_user, :grade) %>
                {{> views/gradebooks/grading_box}}
              <% elsif @submission.user_can_read_grade?(@current_user, session) %>
                <span class="entered_grade">
                  <% if @submission.excused? %>
                    <%= t "Excused" %>
                  <% else %>
                    <%= i18n_grade(round_if_whole @submission.entered_grade) %>
                  <% end %>
                </span>
                <% if !@submission.excused? &&  @assignment.grading_type == 'points' && @assignment.points_possible %>
                  <%= t('/ %{points_possible}', points_possible: I18n.n(round_if_whole @assignment.points_possible))%>
                <% end %>
              <% end %>
            </td>
          </tr>
          <% if can_do(@assignment, @current_user, :grade) || @submission.user_can_read_grade?(@current_user, session)
              points_deducted = I18n.n(-@submission.points_deducted) if @submission.points_deducted&.> 0
              points_possible = I18n.n(round_if_whole(@assignment.points_possible))
              grade_breakdown_style = hidden unless points_deducted && !@submission.excused?
          %>
            <tr class="error late-penalty-display" style="<%= grade_breakdown_style %>">
              <th><%= I18n.t('Late penalty:') %></td>
              <td class="late_penalty bold"><%= points_deducted %></td>
            </tr>
            <tr class="late-penalty-display" style="<%= grade_breakdown_style %>">
              <th><%= I18n.t('Final grade:') %></td>
              <td class="bold">
                <% if can_do(@assignment, @current_user, :grade) %>
                  <span class="published_grade"><%= i18n_grade(round_if_whole @submission.grade) %></span>
                <% elsif @submission.user_can_read_grade?(@current_user, session) %>
                  <span class="grade"><%= i18n_grade(round_if_whole @submission.published_grade) %></span>
                <% end %>

                <% if @assignment.grading_type == 'points' %>
                  / <%= points_possible %>
                <% end %>
              </td>
            </tr>
          <% end %>
        </tbody>
      </table>

        <% if can_do(@submission, @current_user, :view_turnitin_report) %>
          <% if (turnitin_score = @submission.turnitin_data[@submission.asset_string]) &&
            @submission.turnitin_data[:provider] == nil && turnitin_score[:similarity_score] %>
            <span class="turnitin_score_container">
              <span class="turnitin_score_container_caret <%= turnitin_score[:state] %>_score"></span>
              <a href="<%= context_url(@context, :context_assignment_submission_turnitin_report_url, @submission.assignment_id, @submission.user_id, @submission.asset_string) %>" target="_blank" class="not_external turnitin_similarity_score <%= turnitin_score[:state] %>_score"><%= turnitin_score[:similarity_score] %>%</a>
            </span>
          <% end %>
        <% end %>
      <% elsif @submission.can_view_plagiarism_report('vericite', @current_user, session) && !@assignment.muted? %>
        <% if (vericite_score = @submission.vericite_data(true)[@submission.asset_string]) && @submission.turnitin_data[:provider] == :vericite && vericite_score[:similarity_score] %>
          <span class="turnitin_score_container">
            <span class="vericite_score_container_caret <%= vericite_score[:state] %>_score"></span>
            <a href="<%= context_url(@context, :context_assignment_submission_vericite_report_url, @submission.assignment_id, @submission.user_id, @submission.asset_string) %>" target="_blank" title="Similarity score -- more information" class="tooltip not_external turnitin_similarity_score <%= vericite_score[:state] %>_score">
              <%= vericite_score[:similarity_score] %>%
              <span class="tooltip_wrap right">
                <span class="tooltip_text">{{#t}}See VeriCite results{{/t}}</span>
              </span>
            </a>
          </span>
        <% end %>
      <% end %>
      <% if @assignment.rubric_association && (
           @submission.user_can_read_grade?(@current_user, session) ||
           @assignment.rubric_association.user_can_assess_for?(assessor: @current_user, assessee: @submission.user) ||
           can_do(@assignment.rubric, @current_user, :read)
         )
      %>
        <a href="#" class="assess_submission_link Submission__Link--has-icon" style="font-size: 0.8em;" tabindex='0'>
          {{> icons/rubric}}
          {{#t}}Show Rubric{{/t}}
        </a>
      <% end %>
    </div>
    <h2 class="submission_header" style="margin-bottom: 0;"><%= @assessment_request ? t('peer_review', "Peer Review") : t('submission_details', "Submission Details") %></h2>
    <span class="SubmissionDetails__Text"><%= @assignment.title %>, <%= submission_author_name_for @assessment_request %></span>
    <span class="SubmissionDetails__Text <%='late' if @submission.late? %> <%= 'hidden' unless @submission.submitted_at %>">
      <%= t('submitted_at', 'submitted %{submit_date}', :submit_date => datetime_string(@submission.submitted_at)) %>
    </span>

    <% if @context.feature_enabled?(:new_gradebook) %>
      <% if @submission.late? %>
        <span class="submission-late-pill"></span>
      <% elsif @submission.missing? %>
        <span class="submission-missing-pill"></span>
      <% end %>
    <% end %>
    <% if can_do(@context, @current_user, :grade) %>
      <a class="forward" href="<%= context_url(@context, :speed_grader_context_gradebook_url, :assignment_id => @assignment.id) + "#" + CGI::escape({:student_id => @submission.user_id }.to_json) %>" style="float: right; padding-right: 10px;">{{#t}}SpeedGrader™{{/t}}</a>
    <% end %>
    <% if @assignment.expects_submission? && can_do(@assignment, @current_user, :submit) && @current_user == @submission.user %>
      <span><a href="<%= context_url(@context, :context_assignment_url, @assignment.id) %>#submit" class="Button Button--primary" >
        <%= @submission.has_submission? ? t('links.resubmit', "Re-submit Assignment") : t('links.submit', "Submit Assignment") %>
      </a></span>
    <% end %>
    <% if @assessment_request %>
      <div class="assessment_request_completed_message" style="<%= hidden unless @assessment_request.completed? %>">
        {{#t}}You have finished the required steps for this peer review.{{/t}}
      </div>
      <div class="assessment_request_incomplete_message" style="<%= hidden if @assessment_request.completed? %>">
        <% if @assessment_request && @assessment_request.rubric_association %>
          <%= mt('peer_review_not_done_rubric', "**This peer review is not finished yet.**  For it to be considered finished, you need to leave at least one comment and fill out the rubric form to the right.")
          %>
        <% else %>
          <%= mt('peer_review_not_done', "**This peer review is not finished yet.**  For it to be considered finished, you need to leave at least one comment.")
            %>
        <% end %>
      </div>
    <div>
    </div>
    <% end %>
    <div class="clear"></div>
  </div>

  <% if @assignment.rubric_association %>
    <% @visible_rubric_assessments ||= [] %>
  <div id="rubric_holder" style="position: absolute; right: 0px; padding: 5px; background-color: #fff; border: 1px solid #aaa; border-right-width: 0; display: none;">
    <a
      class="hide_rubric_link"
      style="float: right; margin-right:0 !important;"
      title="{{#t}}Hide Rubric{{/t}}"
      role="button"
      aria-label="{{#t}}Hide Rubric{{/t}}"
      tabindex='0'
    >{{> icons/x}}</a>
    <div id="rubric_assessments_list" style="text-align: right; margin-bottom: 5px; <%= hidden if @assessment_request || @visible_rubric_assessments.empty? %>">
      <%= before_label('show_assessment_by', 'Show Assessment By') %>
      <select id="rubric_assessments_select">
        <% @visible_rubric_assessments.each do |assessment| %>
          <option id="rubric_assessment_option_<%= assessment.id %>" value="<%= assessment.id %>">
            <% if can_do(assessment, @current_user, :read_assessor) %>
              <%= assessment.assessor_name %>
            <% else %>
              {{#t}}Anonymous User{{/t}}
            <% end %>
          </option>
        <% end %>
        <% if can_do(@context, @current_user, :manage_grades) && @submission.user != @current_user %>
        <% unless @visible_rubric_assessments.map{|a| a.assessor_id}.include?(@current_user.id) %>
          <option value="new" id="new_rubric_assessment_option">{{#t}}[New Assessment]{{/t}}</option>
        <% end %>
        <% end %>
      </select>
    </div>
    <% if @assignment.rubric.present? %>
      <%= render({
        partial: 'shared/rubric',
        object: @assignment.rubric,
        locals: {
          assessing: @submission.grants_right?(@current_user, :grade) || currently_peer_reviewing
        }}) %>
    <% end %>
    {{> views/shared/rubric_criterion_dialog}}
    <div class="button-container">
      <% if @assignment.rubric_association.user_can_assess_for?(assessor: @current_user, assessee: @submission.user) %>
        <button type="button" class="save_rubric_button btn btn-small">{{#t}}Save Comment{{/t}}</button>
      <% end %>
    </div>
    <a href="<%= context_url(@context, :context_rubric_association_rubric_assessments_url, @assignment.rubric_association) %>" class="update_rubric_assessment_url" style="display: none;">&nbsp;</a>
  </div>
  <% end %>

  <div style="display: none;">
    <% if !currently_peer_reviewing && can_do(@context, @current_user, :manage_grades) %>
      <a style="display: none;" title="POST" href="<%= context_url(@context, :update_submission_context_gradebook_url) %>" class="update_submission_url">&nbsp;</a>
    <% else %>
      <a style="display: none;" title="PUT" href="<%= context_url(@context, :context_assignment_submission_url, @submission.assignment_id, @submission.user_id) %>" class="update_submission_url">&nbsp;</a>
    <% end %>
    <a style="display: none;" href="<%= context_url(@context, :context_assignment_submission_url, @submission.assignment_id, @submission.user_id) %>" class="submission_data_url">&nbsp;</a>
  </div>
  <div class="submission-details-container">
    <div class="submission-details-frame">
      <iframe id="preview_frame" src="<%= context_url(@context, :context_assignment_submission_url, @assignment.id, @submission.user_id, :preview => 1, :rand => rand(999999)) %>" allowfullscreen="true" frameborder="0" style="border: 0; width: 100%; height: 300px; display: block;" title="{{#t}}Preview{{/t}}"></iframe>
    </div>
    <div class="submission-details-comments" style="word-wrap: break-word; min-width: 1%;">
      <div style="background-color: #fff;">
        <a href="#" class="comments_link" style="display: none; <%= 'font-weight: bold;' unless @submission.submission_comments.empty? %>">Comments <% unless @submission.submission_comments.empty? %>(<%= @submission.submission_comments.length %>)<% end %></a>
        <div class="comments">
          <button type="button" class="btn button-secondary cancel_comment_button" style="display: none; float: right; margin-right:0 !important;"><%= image_tag("delete_circle.png", :alt => t('Delete')) %></button>
          <div class="comment_list" aria-live="polite" style="padding: 10px; clear:right;">
            <% unless @submission.user_can_read_grade?(@current_user, session) %>
              <% if @assignment.muted? %>
                <i>{{#t}}You may not see all comments right now because the assignment is currently being graded{{/t}}</i>
              <% else %>
                <i>{{#t}}As a peer reviewing student, you will only see comments written by you.{{/t}}</i>
              <% end %>
              <br/><br/>
            <% end %>
            <% @submission.visible_submission_comments.each do |comment| %>
              <% if can_do(comment, @current_user, :read) %>
              <div class="comment <%= 'draft' if comment.draft? %>" id="submission_comment_<%= comment.id %>" style="margin-bottom: 10px;">
                <div class="comment" style="color: #444;">
                  <% if comment.media_comment_id && comment.media_comment_type %>
                    <div class="comment_media">
                      {{#t}}media_comment_link', "This is a media comment, *click here to view*.", :wrapper => '<br/><a href="#" class="play_comment_link media-comment">\1</a>.{{/t}}
                      <span class="media_comment_id" style="display: none;"><%= comment.media_comment_id %></span>
                      <div class="media_comment_content">
                      </div>
                    </div>
                  <% else %>
                    <span style="white-space: pre-wrap;"><%= comment.comment %></span>
                  <% end %>
                </div>
                <div class="comment_attachments" style="text-align: right; margin-right: 10px;">
                  <% comment.attachments.each do |attachment| %>
                    <div class="comment_attachment">
                      <a href="<%= context_url(@context, :context_assignment_submission_url, @assignment.id, @submission.user_id, :comment_id => comment.id, :download => attachment.id) %>" class="comment_attachment_link <%= attachment.mime_class %>"><%= attachment.display_name %></a>
                    </div>
                  <% end %>
                </div>
                <div style="text-align: right; font-size: 0.8em; color: #888;">
                  <span class="author_name">
                    <%= comment_author_name_for(comment) %>
                  </span>,
                  <span class="posted_at"><%= datetime_string(comment.created_at) %></span>
                </div>
              </div>
              <% end %>
            <% end %>
          </div>
          <div class="comment_media" id="comment_media_blank" style="display: none; white-space: normal;">
            {{#t}}media_comment_link', "This is a media comment, *click here to view*.", :wrapper => '<br/><a href="#" class="play_comment_link media-comment">\1</a>.{{/t}}
            <span class="media_comment_id" style="display: none;">&nbsp;</span>
            <div class="media_comment_content">
            </div>
          </div>
          <div class="comment_attachment" id="comment_attachment_blank" style="display: none;">
            <a class="comment_attachment_link" href="<%= context_url(@context, :context_assignment_submission_url, @assignment.id, @submission.user_id, :comment_id => "{{ comment_id }}", :download => "{{ id }}") %>"><span class="display_name">&nbsp;</span></a>
          </div>
          <div class="comment" id="comment_blank" style="display: none; margin-bottom: 10px;">
            <span class="comment" style="color: #444; white-space: pre-wrap;"></span>
            <div class="comment_attachments" style="text-align: right; margin-right: 10px;"></div>
            <div style="text-align: right; font-size: 0.8em; color: #888;">
              <span class="author_name">&nbsp;</span>,
              <span class="posted_at">&nbsp;</span>
            </div>
          </div>
          <div class="comment_attachment_input nobr" id="comment_attachment_input_blank" style="display: none;">
            <input type="file" name="attachments[-1][uploaded_data]"/>
            <a href="#" class="delete_comment_attachment_link no-hover">{{> icons/end}}<span class="screenreader-only"> t('Delete') </span></a>
          </div>
          <div id="media_media_recording" style="display: none;">
            <div class="media_recording">
            </div>
          </div>
          <div style="padding: 10px; <%= hidden unless can_do(@submission, @current_user, :comment) %>" id="add_comment_form">
            <strong><%= before_label('add_a_comment', 'Add a Comment') %></strong>
            <span><br/>{{#t}}Teachers and submitter will be notified of all comments.{{/t}}</span>
            <textarea class="ic-Input grading_comment"></textarea>
            <div class="comment_attachments">
            </div>
            <div class="clearfix" style="text-align: right;">
              <% if feature_enabled?(:kaltura) %>
                <a href="#" style="float: left;" class="media_comment_link media-comment">{{#t}}Media Comment{{/t}}</a>
              <% end %>
              <a href="#" style="float: right;" class="attach_comment_file_link">{{#t}}Attach File{{/t}}</a>
              <div class="clear"></div>
            </div>
            <% if @assignment.has_group_category? && can_do(@submission, @current_user, :make_group_comment) %>
              <label class="checkbox" for="submission_group_comment">
                <input type="checkbox" name="group_comment" id="submission_group_comment"/>
                {{#t}}Send Comment to the Whole Group{{/t}}
              </label>
            <% end %>
            <div class="button-container">
              <button type="button" class="Button Button--primary save_comment_button">{{#t}}Save{{/t}}</button>
            </div>
          </div>
        </div>
      </div>
    </div>
  </div>
<% js_env :rubricAssessments => @visible_rubric_assessments.as_json(:include => :assessor, :permissions => {:user => @current_user, :session => session}) %>
